<% layout('/layouts/_default.html', {title: '角色管理', libs: ['dataGrid']}){ %>
<div class="main-content">
  <div class="box box-main">
    <div class="box-header">
      <div class="box-title">
        <i class="fa icon-people"></i> 角色授权用户（${role.roleName}-${role.roleCode}）
      </div>
      <div class="box-tools pull-right">
        <a href="#" class="btn btn-default" id="btnAddUser" title="添加用户"><i class="fa fa-plus"></i> 添加用户</a>
        <a href="#" class="btn btn-default" id="btnDelUser" title="删除用户"><i class="fa fa-remove"></i> 批量取消</a>
        <a href="javascript:" class="btn btn-default" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> 关 闭</a>
      </div>
    </div>
    <div class="box-body">
      <#form:form id="searchForm" action="${ctxPath}/admin/listData" method="post" class="form-inline "
          data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
        <#form:hidden name="roleCode" value="${role.roleCode}"/>
        <#form:hidden name="userType" value="${role.userType}"/>
        <div class="form-group">
          <label class="control-label">账号：</label>
          <div class="control-inline">
            <#form:input name="loginCode" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">昵称：</label>
          <div class="control-inline">
            <#form:input name="userName" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">邮箱：</label>
          <div class="control-inline">
            <#form:input name="email" maxlength="300" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">手机：</label>
          <div class="control-inline">
            <#form:input name="mobile" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">电话：</label>
          <div class="control-inline">
            <#form:input name="phone" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <button type="submit" class="btn btn-primary btn-sm">查询</button>
          <button type="reset" class="btn btn-default btn-sm">重置</button>
        </div>
      </#form:form>     
      <table id="dataGrid"></table>
      <div id="dataGridPage"></div>
    </div>
  </div>
</div>
<div class="hide">
<#form:listselect id="userSelect" title="用户选择"
  url="${ctxPath}/admin/userSelect?userType=${role.userType}" allowClear="false" 
  checkbox="true" itemCode="userCode" itemName="userName"/>
</div>
<% } %>
<script>
//初始化DataGrid对象
$('#dataGrid').dataGrid({
  searchForm: $("#searchForm"),
  columnModel: [
    {header:'登录账号', name:'loginCode', index:'a.login_code', width:200, align:"center"},
    {header:'用户昵称', name:'userName', index:'a.user_name', width:200, align:"center"},
    {header:'电子邮箱', name:'email', index:'a.email', width:200, align:"center"},
    {header:'手机号码', name:'mobile', index:'a.mobile', width:200, align:"center"},
    {header:'办公电话', name:'phone', index:'a.phone', width:200, align:"center"},
    {header:'更新时间', name:'updateDate', index:'a.update_date', width:200, align:"center"},
    {header:'状态', name:'status', index:'a.status', width:100, align:"center", formatter: function(val, obj, row, act){
      return js.getDictLabel([{"dictValue":"0","cssStyle":"","cssClass":"","dictLabel":"正常"},{"dictValue":"1","cssStyle":"color:#f00;","cssClass":"","dictLabel":"删除"},{"dictValue":"2","cssStyle":"color:#f00;","cssClass":"","dictLabel":"停用"},{"dictValue":"3","cssStyle":"color:#fa0;","cssClass":"","dictLabel":"冻结"},{"dictValue":"4","cssStyle":"","cssClass":"","dictLabel":"待审"},{"dictValue":"5","cssStyle":"","cssClass":"","dictLabel":"驳回"},{"dictValue":"9","cssStyle":"color:#aaa;","cssClass":"","dictLabel":"草稿"}], val, '未知', true);
    }},
    {header:'操作', name:'actions', width:100, sortable:false, title:false, formatter: function(val, obj, row, act){
      var actions = [];
      //if(row.mgrType != 0){
    	  actions.push('<a href="${ctxPath}/role/deleteAuthUser?roleCode=${role.roleCode}&userRoleString='+row.userCode+'" class="btnList" title="取消授权" data-confirm="确认要取消该用户角色吗？"><i class="fa fa-remove"></i></a>&nbsp;');           
     // }
      return actions.join('');
    }}
  ],
  showCheckbox: true,
  // 加载成功后执行事件
  ajaxSuccess: function(data){
    // add by haodw 20181009 start
    // TODO 分页问题
    var codes=[], names=[];
    if(data&&data.list) {
    	$.each(data.list, function(i, ii) {
    		codes.push(ii.userCode);
    		names.push(ii.userName);
    	});
    }
    $('#userSelectCode').val(codes.join(','));
    $('#userSelectName').val(names.join(','));
    // add by haodw 20181009 end
  }
});
$('#btnDelUser').click(function(){
  var userCodes = $('#dataGrid').dataGrid('getSelectRows');
  if (userCodes != null && userCodes.length > 0){
    js.confirm('确认要取消列表中权重的用户角色权限吗？', function(){
      js.ajaxSubmit('${ctxPath}/role/deleteAuthUser', {
        roleCode: '${role.roleCode}',
        userRoleString: userCodes.join(',')
      }, function(data){
        js.showMessage(data.message);
        page();
      });
    });
  }else{
    js.showMessage('请在列表选中要取消角色的用户');
  }
  return false;
});
$('#btnAddUser').click(function(){
  $('#userSelectName').click();
});
function listselectCallback(id, action, index, layero){
  if (id == 'userSelect' && action == 'ok'){
    if ($('#userSelectCode').val() != ''){
      js.ajaxSubmit('${ctxPath}/role/saveAuthUser', {
        roleCode: '${role.roleCode}',
        userRoleString: $('#userSelectCode').val()
      }, function(data){
        js.showMessage(data.message);
        page();
      });
    }else{
      js.showMessage('没有选择要授权的用户');
    }
  }
}
</script>
